package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.analyst.spatialanalyst.CalculationTerrain;
import com.supermap.analyst.spatialanalyst.SlopeType;
import com.supermap.data.*;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;

public class SlopeAndAspectAnalystDialog extends CJDialog {
    private JComboBox<String> datasourceCmb;
    private JComboBox<String> datasetCmb;
    private JComboBox<String> analystModeCmb;

    private Workspace workspace;

    private ProcessBarDialog processBarDialog;

    public SlopeAndAspectAnalystDialog(Frame owner, String title, boolean modal, Workspace workspace) {
        super(owner, title, modal, 360, 160, new Object[]{workspace});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.workspace = (Workspace) args[0];
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        super.confirmBtn.setText("分析");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        add(mainPanel, BorderLayout.CENTER);

        JLabel datasourceLbl = new JLabel("数据源: ");
        mainPanel.add(datasourceLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        datasourceCmb = new JComboBox<>();
        if (workspace != null) {
            Datasources datasources = workspace.getDatasources();
            if (datasources != null && datasources.getCount() > 0) {
                for (int i = 0; i < datasources.getCount(); i++) {
                    Datasource datasource = datasources.get(i);
                    datasourceCmb.addItem(datasource.getAlias());
                }
            }
        }
        datasourceCmb.addItemListener(e -> {
            if (datasourceCmb.getItemCount() > 0) {
                String selectedDatasourceName = (String) e.getItem();
                Datasources datasources = workspace.getDatasources();
                Datasource datasource = datasources.get(selectedDatasourceName);
                loadDatasetGrid(datasource);
            }
        });
        mainPanel.add(datasourceCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel datasetLbl = new JLabel("数据集: ");
        mainPanel.add(datasetLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        datasetCmb = new JComboBox<>();
        if (datasourceCmb.getItemCount() > 0) {
            Datasource datasource = workspace.getDatasources().get((String) datasourceCmb.getSelectedItem());
            loadDatasetGrid(datasource);
        }
        mainPanel.add(datasetCmb, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel analystModeLbl = new JLabel("分析类型: ");
        mainPanel.add(analystModeLbl, new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        analystModeCmb = new JComboBox<>();
        analystModeCmb.addItem("坡度");
        analystModeCmb.addItem("坡向");
        mainPanel.add(analystModeCmb, new GridBagConstraints(1, 2, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        if (datasourceCmb.getItemCount() > 0) {
            datasourceCmb.setSelectedIndex(0);
        }
        if (datasetCmb.getItemCount() > 0) {
            datasetCmb.setSelectedIndex(0);
        }
        analystModeCmb.setSelectedIndex(0);
    }

    @Override
    protected void doConfirmCore() {
        if (processBarDialog == null) {
            processBarDialog = new ProcessBarDialog((Frame) getOwner(), "坡度坡向分析", true);
            processBarDialog.setValue(0);
        }

        if (datasetCmb.getItemCount() > 0 && datasetCmb.getSelectedIndex() != -1) {
            String datasourceName = (String) datasourceCmb.getSelectedItem();
            String datasetName = (String) datasetCmb.getSelectedItem();
            int analystMode = analystModeCmb.getSelectedIndex();

            Datasources datasources = workspace.getDatasources();
            Datasource datasource = datasources.get(datasourceName);
            DatasetGrid datasetGrid = (DatasetGrid) datasource.getDatasets().get(datasetName);

            switch (analystMode) {
                // 坡度
                case 0:
                    String slopeDatasetName = datasource.getDatasets().getAvailableDatasetName(datasetName + "_slope_result");
                    new Thread(() -> CalculationTerrain.calculateSlope(datasetGrid, SlopeType.DEGREE, 1, datasource, slopeDatasetName, steppedEvent -> {
                        int percent = steppedEvent.getPercent();
                        processBarDialog.setValue(percent);
                        if (percent == 100) {
                            processBarDialog.setVisible(false);
                        }
                    })).start();
                    break;
                // 坡向
                case 1:
                    String aspectDatasetName = datasource.getDatasets().getAvailableDatasetName(datasetName + "_aspect_result");
                    new Thread(() -> CalculationTerrain.calculateAspect(datasetGrid, datasource, aspectDatasetName, steppedEvent -> {
                        int percent = steppedEvent.getPercent();
                        processBarDialog.setValue(percent);
                        if (percent == 100) {
                            processBarDialog.setVisible(false);
                        }
                    })).start();
                    break;
            }

            processBarDialog.setVisible(true);
            setVisible(false);
        }
    }

    private void loadDatasetGrid(Datasource datasource) {
        Datasets datasets = datasource.getDatasets();
        if (datasets.getCount() > 0) {
            datasetCmb.removeAll();
            for (int i = 0; i < datasets.getCount(); i++) {
                Dataset dataset = datasets.get(i);
                if (dataset.getType() == DatasetType.GRID) {
                    datasetCmb.addItem(dataset.getName());
                }
            }
        }
    }
}
